package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.admin.o2o.recommend.entity.O2oRecommendEntity;
import com.eascs.admin.o2o.recommend.service.O2oRecommendService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.DateUtils;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 征信流程
 */
@Service
public class RecMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(RecMsgNotifyServiceImpl.class);

	@Autowired
	O2oRecommendService o2oRecommendService;
	
	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动客户推荐流程通知:[{}]", StatusContexts.WFTYPE_REC);
		
		loanapply = RecFlow(notifyParam,loanapply);
		
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("RECOMMEND_REFUND");
		return list;
	}

	@Override
	public Boolean checkReturn() {
		return Boolean.FALSE;
	}
	
	/**
	 * 客户推荐管理流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 * @return
	 */
	private O2oLoanapplyEntity RecFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapplyEntity) {
		
		O2oRecommendEntity  o2oRecommendEntity = o2oRecommendService.getById(notifyParam.getReferId());
		
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {// 流程通过
			log.info("贷款编号:[{}],客户推荐流程完成,修改为状态为：[{}]", o2oRecommendEntity.getBillCode(), StatusContexts.O2O_ZX_END);
			o2oRecommendEntity.setBillStatus(StatusContexts.WFTYPE_STATUS_CLO);
			o2oRecommendEntity.setShareStartDate(new Date());
			o2oRecommendEntity.setShareEndDate(DateUtils.addDate(new Date(), 30));
			
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {// 流程驳回
			log.info("贷款编号:[{}],客户推荐流程完成,修改为主表状态为：[{}]", o2oRecommendEntity.getBillCode(), StatusContexts.WFTYPE_STATUS_BH);
			o2oRecommendEntity.setBillStatus(StatusContexts.WFTYPE_STATUS_BH);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {// 作废
			log.info("贷款编号:[{}],客户推荐流程完成,修改为主表状态为：[{}]", o2oRecommendEntity.getBillCode(), StatusContexts.O2O_FK_OVER);
			o2oRecommendEntity.setBillStatus(StatusContexts.O2O_FK_OVER);
		}
		int i = o2oRecommendService.updateById(o2oRecommendEntity);;
		if (i > 0) {
			log.info("ReferId:[{}],客户推荐流程完成", notifyParam.getReferId());
		} else {
			log.info("ReferId:[{}],客户推荐流程失败", notifyParam.getReferId());
		}
		return loanapplyEntity;
	}
	
}
